<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@page import="model.Item"%>
<%@page import="dao.ItemManager"%>
<%@page import="model.ItemType"%>
<%@page import="java.util.List"%>
<%@page import="java.util.Arrays"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Intranet Library - Edit item</title>
</head>
<body>
	<%@include file='home.jsp'%>
	<%
	String id=request.getParameter("itemId");
	if(id!=null){
	    int itemId = Integer.parseInt(id);
	    ItemManager itemManager = new ItemManager();
	    Item item = itemManager.fetchItemById(itemId);
	%>
	<h2>Edit item</h2>
	<form action="items" method="post">
		<table>
			<tr>
				<td>Title:</td>
				<td><input type="text" name="title"
					value="<%=item.getTitle()%>" />
				</td>
			</tr>
			<tr>
				<td>Author:</td>
				<td><input type="text" name="author"
					value="<%=item.getAuthor()%>" />
				</td>

			</tr>
			<tr>
				<td>Published:</td>
				<td><input type="text" name="publishDate"
					value="<%=item.getPublishDate()%>" />
				</td>

			</tr>
			<tr>
				<td>Type</td>
				<td><select id="type" name="type">
						<%
						    List<ItemType> itemTypes = Arrays.asList(ItemType.values());
						    for (ItemType type : itemTypes)
						    {
						%>
						<option value="<%=type.getName()%>"><%=type.getName()%></option>
						<%
						    }
						%>
				</select>
				</td>
			</tr>
			<tr>
				<td>Period:</td>
				<td><input type="text" name="maxPeriod"
					value="<%=item.getMaxPeriod()%>" />
				</td>

			</tr>
		</table>
		<input type="hidden" name="id" value="<%=item.getId()%>" /> 
		<input type="hidden" name="edit" /> 
		<input type="submit" value="Save changes" />
	</form>
<%}
	else{%>
	<h2>Add item</h2>
	<form action="items" method="post">
		<table>
			<tr>
				<td>Title</td>
				<td><input type="text" name="title" id="title" /></td>
			</tr>
			<tr>
				<td>Author</td>
				<td><input type="text" name="author" id="author" /></td>

			</tr>
			<tr>
				<td>Published</td>
				<td><input type="text" name="publishDate" id="publishDate" />
				</td>

			</tr>
			<tr>
				<td>Type</td>
				<td><select id="type" name="type">
						<%
						    List<ItemType> itemTypes = Arrays.asList(ItemType.values());
						    for (ItemType type : itemTypes)
						    {
						%>
						<option value="<%=type.getName()%>"><%=type.getName()%></option>
						<%
						    }
						%>
				</select>
				</td>
			</tr>
			<tr>
				<td>Period</td>
				<td><input type="text" name="maxPeriod" id="maxPeriod" /></td>
			</tr>
		</table>
		<input type="hidden" name="add" /> 
		<input type="submit" value="Submit" />
	</form>
	<%} %>
</body>
</html>